//
//  SuggestViewController.m
//  BosidengIphone
//
//  Created by  on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "SuggestViewController.h"
#import "RegexKitLite.h"
#import "AppDelegate.h"
#import "ASIFormDataRequest.h"

@implementation SuggestViewController

@synthesize submitButton;
@synthesize waitView;

@synthesize nameField;
@synthesize emailField;
@synthesize detailField;
@synthesize typeField;
@synthesize phoneField;

@synthesize nameLabel;
@synthesize emailLabel;
@synthesize typeLabel;
@synthesize detailLabel;
@synthesize titleLabel;
@synthesize phoneLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
	[titleLabel release];
	[nameLabel release];
	[emailLabel release];
	[typeLabel release];
	[detailLabel release];
    [phoneLabel release];

	[typeField release];
	[nameField release];
	[emailField release];
	[detailField release];
    [phoneField release];
    
	[submitButton release];
	[waitView release];
     
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	titleLabel.text = [AppDelegate getLanguageString:@"proposals_label_title"];
	nameLabel.text = [AppDelegate getLanguageString:@"proposals_label_name"];
	emailLabel.text = [AppDelegate getLanguageString:@"proposals_label_email"];
    phoneLabel.text = [AppDelegate getLanguageString:@"proposals_label_phone"];
	typeLabel.text = [AppDelegate getLanguageString:@"proposals_label_type"];
	detailLabel.text = [AppDelegate getLanguageString:@"proposals_label_detail"];
	typeField.text = [AppDelegate getLanguageString:@"proposals_text_type_select"];
	[submitButton setTitle:[AppDelegate getLanguageString:@"proposals_button_submit"]
				  forState:UIControlStateNormal];
    
    nameField.text = @"";
	emailField.text = @"";
	detailField.text = @"";
    phoneField.text = @"";
    type_en = @"";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)backgroundTab:(id)sender {
	[nameField resignFirstResponder];
	[emailField resignFirstResponder];
	[detailField resignFirstResponder];
	[typeField resignFirstResponder];
    [phoneField resignFirstResponder];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	//NSLog(@"buttonIndex=%d", buttonIndex);
	if (buttonIndex == 0) {
		typeField.text = [AppDelegate getLanguageString:@"proposals_text_type_complaint"];//@"投诉";
		type_en = @"Complaint";
	} else if (buttonIndex == 1) {
		typeField.text = [AppDelegate getLanguageString:@"proposals_text_type_suggest"];//@"建议";
		type_en = @"Suggest";
	} else if (buttonIndex == 2) {
		typeField.text = [AppDelegate getLanguageString:@"proposals_text_type_consultation"];//@"咨询";
		type_en = @"Consultation";
	}
}

- (IBAction)typeSelect:(id)sender {
	[self backgroundTab:nil];
	UIAlertView *infoView = [[UIAlertView alloc] 
							 initWithTitle:[AppDelegate getLanguageString:@"proposals_type_title"]
							 message:nil
							 delegate:self 
							 cancelButtonTitle:nil
							 otherButtonTitles:
							 [AppDelegate getLanguageString:@"proposals_text_type_complaint"],
							 [AppDelegate getLanguageString:@"proposals_text_type_suggest"],
							 [AppDelegate getLanguageString:@"proposals_text_type_consultation"],
							 [AppDelegate getLanguageString:@"common_button_close"],nil];
	[infoView show];
	[infoView release];
}

- (void)showMessge:(NSString *)message {
	UIAlertView *infoView = [[UIAlertView alloc] 
							 initWithTitle:@""
							 message:[AppDelegate getLanguageString:message]
							 delegate:nil 
							 cancelButtonTitle:[AppDelegate getLanguageString:@"common_button_close"]
							 otherButtonTitles:nil];
	[infoView show];
	[infoView release];
}

- (IBAction)submit:(id)sender {
	[self backgroundTab:nil];
	NSString *name = nameField.text;
	if ([name isEqualToString:@""]) {
		[self showMessge:@"proposals_check_name_null"];
		return;
	}
	
	NSString *email = emailField.text;
	if ([email isEqualToString:@""]) {
		[self showMessge:@"proposals_check_email_null"];
		return;
	}
	BOOL result = [email isMatchedByRegex:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b"];
	if (!result) {
		[self showMessge:@"proposals_check_email_illegal"];
		return;
	}
    
    NSString *phone = phoneField.text;
	if ([phone isEqualToString:@""]) {
		[self showMessge:@"proposals_check_phone_null"];
		return;
	}
	result = [phone isMatchedByRegex:@"\\b([0-9\\-]+)\\b"];
	if (!result) {
		[self showMessge:@"proposals_check_phone_illegal"];
		return;
	}
	
	if ([type_en isEqualToString:@""]) {
		[self showMessge:@"proposals_check_type_null"];
		return;
	}
	
	NSString *detail = detailField.text;
	if ([detail isEqualToString:@""]) {
		[self showMessge:@"proposals_check_detail_null"];
		return;
	}
	
	[waitView startAnimating];
	submitButton.enabled = NO;
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/feedback.ashx", [AppDelegate serviceUrl]]];
    ASIFormDataRequest *detectPost = [[ASIFormDataRequest alloc] initWithURL:url];
    [detectPost addPostValue:name forKey:@"txt_Name"];
    [detectPost addPostValue:email forKey:@"txt_Email"];
    [detectPost addPostValue:type_en forKey:@"drp_ConsultingType"];
    [detectPost addPostValue:detail forKey:@"txt_Content"];
    [detectPost addPostValue:phone forKey:@"txt_MobTel"];
    [detectPost addPostValue:[AppDelegate getLanguage] forKey:@"txt_Language"];
    
    [detectPost setDelegate:self];
    [detectPost setDidFinishSelector:@selector(requestComplete:)];
    [detectPost setDidFailSelector:@selector(requestFailed:)];
    [requestQueue addOperation:detectPost];

	[detectPost release];
}

- (void)requestComplete:(ASIFormDataRequest *)request {
	NSString *result = [request responseString];
	//NSLog(@"detectDone is run return value is %@", result);
    if ([result rangeOfString:@"html"].length > 0) {
		//[self showMessge:@"Regist Failed!"];
        NSLog(@"Post Suggest Request Failed! result=%@", result);
        return;
    }
    
	NSArray *resultInfos = [result componentsSeparatedByString:@"|"];
    NSString *resultCode = [resultInfos objectAtIndex:0];
    //NSString *resultMessage = [resultInfos objectAtIndex:1];
	if ([@"1" isEqualToString:resultCode]) {
		[self showMessge:@"proposals_submit_success"];
		nameField.text = @"";
		emailField.text = @"";
		typeField.text = [AppDelegate getLanguageString:@"proposals_text_type_select"];
		type_en = @"";
		detailField.text = @"";
        phoneField.text = @"";
	} else {
		[self showMessge:@"proposals_submit_failed"];
        NSLog(@"Post Request Failed! result=%@", result);
	}
    [waitView stopAnimating];
	submitButton.enabled = YES;
}

- (void)requestFailed:(ASIFormDataRequest *)request {
    [self showMessge:@"proposals_submit_failed"];
    NSLog(@"Post Request Failed! error=%@", [request error]);
    [waitView stopAnimating];
	submitButton.enabled = YES;
}


@end
